﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics.Contracts;
using System.Diagnostics;

namespace ODataTestLauncherUI
{
    public partial class Form1 : Form
    {
        private string TestConfigFolderName = "TestConfigs";
        private DirectoryInfo TestResultFolder = new DirectoryInfo(Path.Combine(Environment.CurrentDirectory, "TestResults"));

        List<Process> TestEngineConsoles = new List<Process>();
        public Form1()
        {
            InitializeComponent();
            ReFreshUI();


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string configPath = Path.Combine(Environment.CurrentDirectory, TestConfigFolderName);
            DirectoryInfo dirInf = new DirectoryInfo(configPath);
            foreach (FileInfo file in dirInf.GetFiles("*TestConfig.xml"))
            {
                cmbTestConfigList.Items.Add(file.Name);
            }

        }

        private void btnEditConfigFile_Click(object sender, EventArgs e)
        {
            Contract.Requires(!string.IsNullOrEmpty(cmbTestConfigList.Text), "Please Select a valid configuration file");
            string configFilePath = Path.Combine(Environment.CurrentDirectory, TestConfigFolderName, cmbTestConfigList.Text);
            System.Diagnostics.Process.Start("Notepad.exe", configFilePath);
        }

        private void btnViewConfiguration_Click(object sender, EventArgs e)
        {
            Contract.Requires(!string.IsNullOrEmpty(cmbTestConfigList.Text), "Please Select a valid configuration file");

            string configFilePath = Path.Combine(Environment.CurrentDirectory, TestConfigFolderName, cmbTestConfigList.Text);
            System.Diagnostics.Process.Start("iexplore.exe", configFilePath);
        }

        private void btnExecuteTest_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(cmbTestConfigList.Text))
            {

                Contract.Requires(!string.IsNullOrEmpty(cmbTestConfigList.Text), "Please Select a valid configuration file");
                //string configFilePath = Path.Combine(TestConfigFolderName, cmbTestConfigList.Text);
                string executerPath = Path.Combine(Environment.CurrentDirectory, "ODataServiceRegressionTestToolConsole");
                Process engine = Process.Start(executerPath, cmbTestConfigList.Text);

                TestEngineConsoles.Add(engine);

                timer1.Start();
            }
            else
            {
                MessageBox.Show("Please Select a Test Script");
            }
        }


        private void btnBrowseResult_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(Path.Combine(Environment.CurrentDirectory, "TestResults"));
        }

        private void ReFreshUI()
        {
            lbResultFiles.Items.Clear();
            foreach (FileInfo file in TestResultFolder.GetFiles("*TestResult*", SearchOption.TopDirectoryOnly))
            {
                lbResultFiles.Items.Add(file);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (Process proc in TestEngineConsoles)
            {
                if (proc.HasExited)
                {
                    ReFreshUI();
                    TestEngineConsoles.Remove(proc);
                    break;
                }
            }

            if (TestEngineConsoles.Count == 0)
            {
                timer1.Stop();
                lblStatus.Text = "Finished/Ready";
            }

            lblStatus.Text = "Excuting Test   :" + DateTime.Now;
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            string helpFile = Path.Combine(Environment.CurrentDirectory, "Docs", "UserGuide.docx");
            Process.Start(helpFile);
        }

        private void lbResultFiles_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string filename = ((FileInfo)lbResultFiles.SelectedItem).FullName;
            if (filename.ToUpper().EndsWith(".XML"))
            {
                System.Diagnostics.Process.Start("iexplore.exe", filename);
            }
            else
            {
                System.Diagnostics.Process.Start(filename);
            }

        }

        private void btnErrorLog_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("Error.log");
        }
    }
}
